package com.jun.staffsystem.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jun.staffsystem.common.Result;
import com.jun.staffsystem.entity.Staff;
import com.jun.staffsystem.mapper.StaffMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/*
 * @Author: jun
 * @Date:2022/11/27 18:46
 * @概述：
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Resource
    StaffMapper staffMapper;

    /**
     * 添加员工
     * @param staff
     * @return
     */
    @PostMapping
    public Result<?> add(@RequestBody Staff staff){
        staffMapper.insert(staff);
        return Result.success();
    }

    /**
     * 渲染页面
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){

        LambdaQueryWrapper<Staff> wrapper = Wrappers.<Staff>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Staff::getStaffName, search);
        }
        Page<Staff> p = staffMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(p);
    }

    /**
     * 更新员工信息
     * @param staff
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestBody Staff staff){
        staffMapper.updateById(staff);
        return Result.success();
    }

    /**
     * 删除员工
     * @param sid
     * @return
     */
    @DeleteMapping("/{sid}")
    public Result<?> delete(@PathVariable Long sid){
        staffMapper.deleteById(sid);
        return Result.success();
    }

}
